package trabalhoSD.src;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class PerfectPointToPointLinks extends Socket{
	private Socket pl;
	private DataInputStream in;
	private DataOutputStream out;
	private ServerSocket serve;
	
	public PerfectPointToPointLinks(int port) throws IOException{//servidor
		serve = new ServerSocket(port);
		
		pl = serve.accept();
		try {
			in = new DataInputStream(pl.getInputStream());
			out = new DataOutputStream(pl.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public PerfectPointToPointLinks(String address, int port){//cliente
		
		try {
			pl = new Socket(address, port);
			in = new DataInputStream(pl.getInputStream());
			out = new DataOutputStream(pl.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void Send(int msg){
		try {
			out.writeInt(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public int Deliver(){
		try {
			return in.readInt();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	public void ClintClose(){
		try {
			in.close();
			out.close();
			pl.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void ServClose(){
		try {
			in.close();
			out.close();
			pl.close();
			serve.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

}
